/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function(nums) {
    nums.sort((v1,v2)=>v1-v2);
    if(nums.length<3)return [];
    //console.log(nums);
    let result = new Map();
    let length = nums.length;
    for(let i=0;i<length-2;i++){
        if(result.has([nums[i],nums[i+1],nums[i+2]].join()))continue;
        if(nums[i]+nums[i+1]+nums[i+2]>0)break;

        let j = i+1;
        let k = length-1;

        while(j<k){
            let sum = nums[i]+nums[j]+nums[k];
            if(sum>0){
                k--;
            }
            else if(sum<0){
                j++;
            }
            else{
                let key = [nums[i],nums[j],nums[k]].join();
                result.set(key, [nums[i],nums[j],nums[k]]);
                j++;
                k--;
            }
        }
    }
    return Array.from(result.values());
};

console.log(threeSum([-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0]));
